package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.dto.AirAddDTO;
import com.qf.dto.AirUpdateDTO;
import com.qf.entity.AirQualityIndex;
import com.qf.service.AirQualityIndexService;
import com.qf.vo.AirVO;
import com.qf.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project work
 * @Package com.qf.controller
 * @Date 2021/10/9 11:19
 */
@RestController
@CrossOrigin(allowCredentials = "true")
@RequestMapping("/air")
public class AirQualityIndexController {

    @Autowired
    private AirQualityIndexService airQualityIndexService;

    @RequestMapping("/list")
    public R findAll(@RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(value = "limit",defaultValue = "3") Integer row,
                     Integer districtId){
        PageInfo<AirQualityIndex> pageInfo = airQualityIndexService.findByDistrictIdAndPage(page, row, districtId);
        //获取总条数
        long count = pageInfo.getTotal();
        // 获取list，拿到data中需要的数据 （需要将List<AirQualityIndex>，转换为List<VO>）
        List<AirVO> data = new ArrayList<>();
        for (AirQualityIndex airQualityIndex : pageInfo.getList()) {
            AirVO airVO = new AirVO();
            BeanUtils.copyProperties(airQualityIndex,airVO);
            airVO.setDistrictName(airQualityIndex.getDistrict().getName());
            data.add(airVO);
        }
        return R.ok(count,data);
    }

    @RequestMapping("/add")
    public R add(@Valid AirAddDTO airAddDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            System.out.println("【添加空气质量信息】  参数不合法   msg = "+msg);
            return R.error(18,msg);
        }
        AirQualityIndex airQualityIndex = new AirQualityIndex();
        BeanUtils.copyProperties(airAddDTO,airQualityIndex);
        airQualityIndexService.insert(airQualityIndex);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(Integer id){
        if(id == null){
            String msg = "操作不合法！";
            System.out.println("【删除空气质量信息】  参数不合法   msg = " + msg);
            return R.error(18,msg);
        }
        airQualityIndexService.deleteById(id);
        return R.ok();
    }

    @RequestMapping("update")
    public R update(@Valid AirUpdateDTO airUpdateDTO,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            System.out.println("【修改空气质量信息】  参数不合法   msg = "+msg);
            return R.error(18,msg);
        }
        AirQualityIndex airQualityIndex = new AirQualityIndex();
        BeanUtils.copyProperties(airUpdateDTO,airQualityIndex);
        airQualityIndexService.updateById(airQualityIndex);
        return R.ok();
    }
}
